var express = require('express') ;
var router = express.Router() ;
var callfile = require('child_process');

/* POST */
//for apusicUI
router.post('/handler' , function(req,res,next){
  console.log("webhook handler") ;
  console.log("event:",req.headers["X-Gitee-Event"]) ;
  console.log("hook_name:",req.body["hook_name"]) ;
  console.log("pull_request:",req.body.pull_request?.state) ;
  if(
    req.body["hook_name"]==="merge_request_hooks"
    &&
    req.body.pull_request?.state==="merged"
  ){
    callfile.execFile("./routes/webhook/aui-build-site.sh",function(error,stdout,stderr){
      console.log(error,stdout,stderr) ;
    }) ;
    return res.send("合并后触发构建!") ;
  }
  res.send('ok') ;
}) ;

/*POST*/
//for docs center
router.post('/docs-center-handler',function(req,res,next){
  console.log("docs-center webhook handler");
  callfile.execFile("./routes/webhook/docs-center-build.sh",function(error,stdout,stderr){
    console.log(error,stdout,stderr) ;
  }) ;
  return res.send("合并后触发构建!") ;

})

module.exports = router ;
